<?php

namespace app\index\controller;
use think\Db;
use \think\Cookie;
use \think\Model;
use think\Session;
class User extends Model
{
    protected $createTime = 'regtime';
    protected $updateTime = 'lasttime';
}
class Login extends \think\Controller
{
    public function login()
    {
        if (!file_exists('install.lock')) {
            echo "<script language='javascript' type='text/javascript'>";
            echo "window.location.href='install/install/install'";
            echo "</script>";
        }
        if (Cookie::get('safecode')) {
            $this->redirect('home/Home/home');
        }
        if (input('param.')) {
            $in_put = input('param.');
            $search = new User();
            $search_data = $search->where('name', $in_put['name'])->where('password', md5($in_put['password']))->find();
            if ($search_data) {
                $randcode = random(20, 'all');
                $user = User::get($search_data['Id']);
                $user->safecode = $randcode;
                $user->save();
                cookie(['prefix' => '', 'expire' => 36000]);
                cookie('safecode', $randcode, '36000');
                $this->success('登陆成功哦', '../../home/home/home');
            } else {
                $this->error('用户名或者密码不对哦');
            }
        }
        $webdate = Db::name('index')->getById(1);//根据用户识别码
        //渲染模板
        $this->assign('name', $webdate['name']);
        $this->assign('copy', $webdate['copy']);
        return $this->fetch('index/login', [], ['__PUBLIC__' => '/public/static/home']);
    }

}
